package com.gl.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gl.entity.EduCourse;
import com.gl.entity.EduCourseDescription;
import com.gl.entity.EduSubject;
import com.gl.entity.EduTeacher;
import com.gl.entity.chapterOrVideo.Chapter;
import com.gl.entity.course.CoursePublish;
import com.gl.mapper.EduCourseMapper;
import com.gl.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-14
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduSubjectService subjectService;
    @Autowired
    private EduCourseDescriptionService descriptionService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduTeacherService teacherService;

    //分页按条件查询课程
    @Override
    public Map<String,Object> pageCourse(Page<EduCourse> page, EduCourse course) {
        //条件
        String subjectParentId = course.getSubjectParentId();//一级分类
        String subjectId = course.getSubjectId();//二级分类
        Long buyCount = course.getBuyCount();//销售量
        Long viewCount = course.getViewCount();//播放次数


        //判断条件是否是空，不是空加上条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(subjectParentId)){
            wrapper.eq("subject_parent_id",subjectParentId);
        }
        if(!StringUtils.isEmpty(subjectId)){
            wrapper.eq("subject_id",subjectId);
        }
        if(!StringUtils.isEmpty(buyCount)){
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(viewCount)){
            wrapper.orderByDesc("view_count");
        }
        //分页查
        baseMapper.selectPage(page,wrapper);
        //把数据存入map中
        Map<String,Object> map = new HashMap<>();
        map.put("list",page.getRecords());//分页集合
        map.put("total",page.getTotal());//总记录数
        map.put("size",page.getSize());//每页大小
        map.put("current",page.getCurrent());//当前页
        map.put("pages",page.getPages());//总页数
        map.put("hasNext",page.hasNext());//是否有下一页
        map.put("hasPrevious",page.hasPrevious());//是否有上一页

        return map;
    }
    //根据课程id查询课程详情
    @Override
    public Map<String, Object> detailByCourseId(String courseId) {
        Map<String,Object> map = new HashMap<>();
        //根据课程id查询课程
        EduCourse eduCourse = baseMapper.selectById(courseId);
        map.put("eduCourse",eduCourse);
        //根据课程里的一级分类id和二级分类id查出对应的一级分类名称和二级分类名称
        EduSubject oneSubject = subjectService.getById(eduCourse.getSubjectParentId());
        EduSubject twoSubject = subjectService.getById(eduCourse.getSubjectId());
        if(oneSubject!=null){
            map.put("oneSubjectName",oneSubject.getTitle());
        }
        if(twoSubject!=null){
            map.put("twoSubjectName",twoSubject.getTitle());
        }
        //查询这个课程是哪个讲师讲的
        EduTeacher teacher = teacherService.getById(eduCourse.getTeacherId());
        map.put("teacher",teacher);

        //根据课程id查询课程描述
        EduCourseDescription eduCourseDescription = descriptionService.getById(eduCourse.getId());
        if(eduCourseDescription!=null){
            map.put("description",eduCourseDescription.getDescription());
        }
        //根据课程id查询对应的章节小节
        List<Chapter> chapterList = chapterService.getChapterVideoByCourseId(courseId);
        map.put("chapterList",chapterList);
        return map;
    }
    //添加课程
    @Override
    public String saveCourse(CoursePublish coursePublish) {
        EduCourse course = new EduCourse();
        BeanUtils.copyProperties(coursePublish,course);
        //向课程表添加一条课程记录
        baseMapper.insert(course);
        String courseId = course.getId();//添加后课程生成课程id

        //向课程描述表添加这个课程的简介
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(courseId);
        courseDescription.setDescription(coursePublish.getDescription());
        descriptionService.save(courseDescription);


        return courseId;
    }
    //课程发布时根据课程id查询最终信息
    @Override
    public CoursePublish getFinalInfoByCourseId(String id) {
        CoursePublish coursePublish = new CoursePublish();
        //查询课程信息
        EduCourse course = baseMapper.selectById(id);
        BeanUtils.copyProperties(course,coursePublish);
        //查询老师信息
        EduTeacher teacher = teacherService.getById(course.getTeacherId());
        coursePublish.setTeacherName(teacher.getName());
        //查询分类信息
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("id",course.getSubjectId());
        wrapper.eq("parent_id",course.getSubjectParentId());
        wrapper.select("title");
        EduSubject subject = subjectService.getOne(wrapper);
        if(subject!=null){
            String title = subject.getTitle();
            coursePublish.setSubjectName(title);
        }

        return coursePublish;
    }
}
